//
// Created by 618lf on 2022/6/21.
//

#ifndef NATIVEIO_NATIVEIO_H
#define NATIVEIO_NATIVEIO_H

#endif //NATIVEIO_NATIVEIO_H

#if defined(_WIN32)
#undef UNIX
#define WINDOWS
#else
#undef WINDOWS
#define UNIX
#endif

/**
 * Unix definitions
 */
#ifdef UNIX
#include <dlfcn.h>
#include <jni.h>
#endif

/**
 * Windows definitions
 */
#ifdef WINDOWS

/* Force using Unicode throughout the code */
#ifndef UNICODE
#define UNICODE
#endif

#include <Windows.h>
#include <stdio.h>
#include <jni.h>

#endif

/* A helper macro to 'throw' a java exception. */
#define THROW(env, exception_name, message) \
  { \
    jclass ecls = (*env)->FindClass(env, exception_name); \
    if (ecls) { \
      (*env)->ThrowNew(env, ecls, message); \
      (*env)->DeleteLocalRef(env, ecls); \
    } \
  }

/* Helper macro to return if an exception is pending */
#define PASS_EXCEPTIONS(env) \
  { \
    if ((*env)->ExceptionCheck(env)) return; \
  }

#define PASS_EXCEPTIONS_GOTO(env, target) \
  { \
    if ((*env)->ExceptionCheck(env)) goto target; \
  }

#define PASS_EXCEPTIONS_RET(env, ret) \
  { \
    if ((*env)->ExceptionCheck(env)) return (ret); \
  }

#define CHECK_DIRECT_BUFFER_ADDRESS(buf) \
  { \
    if (!buf) { \
      THROW(env, "java/lang/UnsupportedOperationException", \
        "JNI access to direct buffers not available"); \
      return; \
    } \
  }
  